<template>
	<view>
		<view class="tabs-box">
			<u-tabs 
				:list="tabList" 
				lineWidth="0" 
				inactiveStyle="font-size: 28rpx;"
				activeStyle="color: #19be6b;font-weight: 900;font-size: 28rpx;"
				itemStyle="padding: 0;width: 25%;height: 80rpx;"
				@change="tabChange"
			></u-tabs>
		</view>
		
		<view class="select-box row-box body_box">
			<!-- <view class="item-box row-box" @click="handelSelectd(0)">
				<text>选择回收商</text>
				<text style="opacity: 0;">H</text>
				<u-icon color="#606266" size="24rpx" name="arrow-down-fill"></u-icon>
			</view> -->
			
			<view class="item-box row-box" @click="handelSelectd(1)">
				<text>报价区间</text>
				<text style="opacity: 0;">H</text>
				<u-icon color="#606266" size="24rpx" name="arrow-down-fill"></u-icon>
			</view>
			
			<view class="item-box row-box" @click="handelSelectd(2)">
				<text>订单查询</text>
				<text style="opacity: 0;">H</text>
				<u-icon color="#606266" size="24rpx" name="arrow-down-fill"></u-icon>
			</view>
		</view>
		
		<view class="list-box">
			<view class="item-box" v-for="(item, key) in goodsList" @click="gotoDetail(item.inquiryOrderId)">
				<view class="row-box title-box">
					<view style="width: 80%;">{{item.recycleBusName}}</view>
					<view style="width: 20%;text-align: right;">
						{{item.statusDesc}}
					</view>
				</view>
				<view class="row-box date-box">
					<view style="width: 60%;">店员：{{item.operatorName}}</view>
					<view style="width: 40%;text-align: right;">{{item.createTime}}</view>
				</view>
				
				<!-- 基本信息 -->
				<view class="info-box row-box">
					<view>
						<image style="width: 120rpx;height: 120rpx;" :src="item.fileUrls[0]"></image>
					</view>
					<view style="margin-left: 20rpx;">
						<view style="font-size: 32rpx;font-weight: 900;">{{item.modelName}}</view>
						<view>{{specsTran(item.specs)}}</view>
					</view>
				</view>
				
				<view style="border-top: #c0c4cc 1px solid;"></view>
				
				<view class="row-box" style="padding-top:16rpx;">
					<view class="center-box">
						<image style="width: 40rpx;height: 40rpx;" src="../../static/img/saoma.png"></image>
					</view>
					<view style="margin-left: 20rpx;font-size: 28rpx;">回收码：{{item.code ? item.code : '无'}}</view>
				</view>
			</view>
		</view>
		
		<view @click="nextPage()">
			<u-divider :text="hasNextPage() ? '加载更多':'没有更多了'"></u-divider>
		</view>
		
		<!-- 条件查询框 -->
		<u-popup 
			:show="show" 
			@close="popupClose" 
			@open="popupOpen" 
			mode="center" 
			customStyle="width:100vw;border-radius: 0 0 30rpx 30rpx;position: fixed;top: 175rpx;"
		>
			<view class="selectd-box" v-show="selectd === 0">
				<view class="h2-font">选择商家</view>
				<view style="margin-top: 20rpx;">
					<u-search placeholder="请输入关键字" :showAction="false" v-model="queryParams.searchValue"></u-search>
					<scroll-view class="scroll-box" :scroll-y="true">
						<view v-for="(item, index) in radios" style="display: inline-block;margin: 10rpx;">
							<u-tag 
								:text="item.name" 
								:plain="true" 
								:type="item.checked ? 'success':'info'" 
								:name="index" 
								@click="radioClick"
								shape="circle"
							>
							</u-tag>
						</view>
						
					</scroll-view>
				</view>
			</view>
			<view class="selectd-box" v-show="selectd === 1">
				<view class="h2-font">报价区间</view>
				<view class="row-box center-box" style="margin-top: 20rpx;">
					<view style="width: 240rpx;">
						<u--input 
							v-model="queryParams.startAmount"
							type="number" 
							shape="circle" 
							inputAlign="center" 
							placeholder="起始金额" 
							customStyle="background-color: #f4f4f5;"></u--input>
					</view>
					<view style="width: 80rpx;text-align: center;">~</view>
					<view style="width: 240rpx;">
						<u--input 
							v-model="queryParams.endAmount"
							type="number" 
							shape="circle" 
							inputAlign="center" 
							placeholder="截止金额" 
							customStyle="background-color: #f4f4f5;"></u--input>
					</view>
				</view>
			</view>
			<view class="selectd-box" v-show="selectd === 2">
				<view class="h2-font">订单查询</view>
				<view>
					<view style="font-size: 24rpx;color: #909399;padding: 20rpx 0;">
						支持:订单编号、回收码、运单号、店员名称、门店名称、商家名型号 关键词查询称、串码、品牌
					</view>
					<u-search placeholder="请输入关键字" :showAction="false" v-model="queryParams.keyword"></u-search>
				</view>
			</view>
			
			<view class="row-box" style="justify-content: center;align-items: center;">
				<view style="width: 30%;">
					<u-button text="重置" @click="reset()"></u-button>
				</view>
				<view style="width: 10%;height: 20rpx;"></view>
				<view style="width: 30%;">
					<u-button type="success" text="确定" @click="searchFlush"></u-button>
				</view>
			</view>
			<view style="height: 50rpx;"></view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				//页面类型 collect = 回收订单；refund = 退单；difference=差异订单
				pageType: 'collect',
				goodsList: [],
				queryParams: {
					pageNum: 1,
					pageSize: 20,
					startAmount: 0,
					endAmount: 0,
					keyword: null,
					// 搜索值
					searchValue: null,
					status: undefined
				},
				radios: [],
				show: false,
				//条件框下标
				selectd: 0,
				total: 0,
				//头部下标
				tabList: [{name: '待发货'},{name: '待收货'},{name: '已完成'},{name: '已取消'}]
			}
		},
		onLoad(params) {
			this.pageType = params.type ? params.type : this.pageType;
			this.getPageList();
		},
		mounted() {
			if(this.pageType === 'collect')
				this.getRecycleList();
		},
		created() {
			uni.$on("orderFlush", () => {
				console.log("监听事件，刷新订单页面")
				this.searchFlush();
			})
		},
		methods: {
			reset() {
				this.queryParams = {
					pageNum: 1,
					pageSize: 20,
					startAmount: 0,
					endAmount: 0,
					keyword: null,
					// 搜索值
					searchValue: null,
					status: undefined
				}
				this.searchFlush();
			},
			/* 加载下一页 */
			nextPage() {
				console.log("加载下一页")
				this.queryParams.pageNum++;
				if(this.hasNextPage())
					this.getPageList();
			},
			/* 是否还有下一页 */
			hasNextPage() {
				return this.goodsList.length < this.total;
			},
			specsTran(e) {
				if(e){
					return e.replaceAll(",", " ");
				}
				return "";
			},
			/* 刷新 */
			searchFlush() {
				this.queryParams.pageNum = 1;
				this.goodsList = [];
				this.getPageList();
			},
			/* 同意刷新接口 */
			getPageList() {
				if(this.pageType === 'refund'){
					//退单
					this.getRefundList();
				}
				if(this.pageType === 'difference') {
					//差异订单
					this.getDifferenceList();
				}
				if(this.pageType === 'collect')
					this.getRecycleList();
			},
			/* 获取回收订单 */
			getRecycleList() {
				console.log("回收订单初始化")
				switch(this.tabIndex) {
					case 0: this.queryParams.status = 2;break;
					case 1: this.queryParams.status = 3;break;
					case 2: this.queryParams.status = 4;break;
					case 3: this.queryParams.status = 5;break;
				}
				this.$request("/appShopApi/order/queryRecycleOrderList", "post", this.queryParams).then(res => {
					this.total = res.total;
					this.goodsList.push(...res.rows);
					this.popupClose();
				})
			},
			/* 退单初始化 */
			getRefundList() {
				//退单
				this.tabList = [{name: '待审核'},{name: '待完成'},{name: '已退单'},{name: '已取消'},]
				this.setQueryParamsStatus();
				this.$request("/appShopApi/order/queryRefundOrderList", "post", this.queryParams).then(res => {
					this.total = res.total;
					this.goodsList.push(...res.rows);
					this.popupClose();
				})
			},
			setQueryParamsStatus() {
				switch(this.tabIndex) {
					case 0: this.queryParams.status = 1;break;
					case 1: this.queryParams.status = 2;break;
					case 2: this.queryParams.status = 3;break;
					case 3: this.queryParams.status = 4;break;
				}
			},
			/* 差异订单初始化 */
			getDifferenceList() {
				this.tabList = [{name: '待审核'},{name: '待处理'},{name: '已完成'},{name: '已取消'}]
				console.log("差异初始化")
				this.setQueryParamsStatus();
				this.$request("/appShopApi/order/querySpreadOrderList", "post", this.queryParams).then(res => {
					this.total = res.total;
					this.goodsList.push(...res.rows);
					this.popupClose();
				})
			},
			/* 状态选择 */
			tabChange(e) {
				this.tabIndex = e.index
				this.searchFlush();
			},
			radioClick(name) {
				console.log("当前name:",name)
				this.radios.map((item, index) => {
					item.checked = index === name ? true : false
				})
			},
			popupClose() {
				this.show = false;
			},
			handelSelectd(e) {
				this.show = true;
				this.selectd = e;
			},
			popupOpen() {
				console.log("打开")
			},
			//详情
			gotoDetail(id) {
				uni.navigateTo({
					url: "/pages/store/inquiry/details?id=" + id,
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.tabs-box {
		background-color: #fff;
		position: fixed;
		z-index: 99999;
		width: 100vw;
		top:0;
	}
	.select-box {
		color: #606266;
		font-size: 26rpx;
		position: fixed;
		z-index: 99999;
		top: 80rpx;
		background-color: #f2f8fc;
		.item-box {
			background-color: #dbf1e1;
			width: 200rpx;
			justify-content: center;
			align-items: center;
			padding: 15rpx;
			border-radius: 40rpx;
			line-height: 40rpx;
			margin-right: 16rpx;
		}
	}
	.selectd-box {
		margin-top: 30rpx;
		padding: 28rpx;
	}
	.scroll-box {
		margin-top: 20rpx;
		height: 230rpx;
	}
	.list-box {
		margin-top: 200rpx;
		.item-box {
			background-color: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			margin: 30rpx;
			line-height: 60rpx;
			.title-box {
				font-size: 32rpx;
				font-weight: 600;
			}
			.date-box {
				font-size: 22rpx;
			}
			.info-box {
				margin-top: 20rpx;
			}
		}
	}
</style>
